package com.five.dao.impl.backstage;

import com.five.dao.backstage.RoomFacDao;
import com.five.entity.backstage.TRoomFacility;
import com.five.entity.backstage.TStaff;
import com.five.util.DBUtil;
import com.five.util.Page;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 常鹏宇
 * @Description:
 * @Date: 2020/11/12 18:55
 */
@Repository
public class RoomFacDaoImpl implements RoomFacDao {
    @Override
    public List<Map> selectRoomFac(Page pageInfo, TRoomFacility tRoomFacility) {
        String sql = "select r.id,m.id roomid,m.roomcode,f.id facilityid,f.name fname,r.status rs\n" +
                        "from t_room_facility r\n" +
                        "left join t_room m\n" +
                        "on r.roomid=m.id\n" +
                        "left join t_facility f\n" +
                        "on r.facilityid = f.id\n" +
                        "where 1=1";
        if (tRoomFacility.getSroomcode()!=null && tRoomFacility.getSroomcode()!=""){
            sql+=" and roomcode like '%"+tRoomFacility.getSroomcode()+"%'";
        }
        if (tRoomFacility.getSfname()!=null && tRoomFacility.getSfname()!=""){
            sql+=" and name like '%"+tRoomFacility.getSfname()+"%'";
        }
        sql+=" group by r.id limit ?,?";
        int pagesize = pageInfo.getLimit();
        int star = (pageInfo.getPage()-1)*pagesize;
        List<Map> maps = DBUtil.queryMap(sql, star, pagesize);
        return maps;
    }

    @Override
    public int addRoomFac(TRoomFacility tRoomFacility) {
        if(tRoomFacility.getId()==null){
            String sql = "insert into t_room_facility(roomid,facilityid,status) values(?,?,?)";
            return DBUtil.manage(sql,tRoomFacility.getRoomid(),tRoomFacility.getFacilityid(),tRoomFacility.getRs());
        }else {
            String sql = "update t_room_facility set roomid = ?,facilityid=?,status=? where id =?";
            return DBUtil.manage(sql,tRoomFacility.getRoomid(),
                    tRoomFacility.getFacilityid(),
                    tRoomFacility.getRs(),
                    tRoomFacility.getId());
        }
    }
    @Override
    public int delRoomFac(TRoomFacility tRoomFacility) {
        String sql = "delete from t_room_facility where id = ?";
        return DBUtil.manage(sql,tRoomFacility.getId());
    }

    @Override
    public List<Map> selectrid() {
        String sql = "select id,roomcode from t_room where status=0 ";
        return DBUtil.queryMap(sql);
    }

    @Override
    public List<Map> selectFname() {
        String sql = "select * from t_facility where status=0 ";
        return DBUtil.queryMap(sql);
    }
    @Override
    public int count(TRoomFacility tRoomFacility) {
        String sql ="select count(*) a\n" +
                    "from t_room_facility r\n" +
                    "left join t_room m\n" +
                    "on r.roomid=m.id\n" +
                    "left join t_facility f\n" +
                    "on r.facilityid = f.id\n" +
                    "where 1=1";
        if (tRoomFacility.getSroomcode()!=null && tRoomFacility.getSroomcode()!=""){
            sql+=" and roomcode like '%"+tRoomFacility.getSroomcode()+"%'";
        }
        if (tRoomFacility.getSfname()!=null && tRoomFacility.getSfname()!=""){
            sql+=" and name like '%"+tRoomFacility.getSfname()+"%'";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String a = maps.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
}
